#include <iostream>
#include <set>
#include <vector>
#include <utility>

using std::cout;
using std::endl;
using std::set;
using std::vector;
using std::pair;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

void test()
{
    set<int> number{1, 3, 5, 8, 9, 4, 5, 6, 5, 3};
    /* set<int, std::greater<int>> number{1, 3, 5, 8, 9, 4, 5, 6, 5, 3}; */
    display(number);

    cout << endl << "set的查找操作" << endl;
    size_t cnt = number.count(1);
    cout << "cnt = " << cnt << endl;

    auto it = number.find(7);
    if(it == number.end())
    {
        cout << "该元素不存在set中" << endl;
    }
    else
    {
        cout << "该元素存在set中 " << *it << endl;
    }

    cout << endl << "set的insert操作" << endl;
    pair<set<int>::iterator, bool> ret = number.insert(100);
    if(ret.second)
    {
        cout << "插入成功 " << *ret.first << endl;
    }
    else
    {
        cout << "插入失败，该元素存在set中" << endl;
    }
    display(number);

    cout << endl << endl;
    vector<int> vec = {11, 44, 99, 66};
    number.insert(vec.begin(), vec.end());
    display(number);

    cout << endl << endl;
    number.insert({7, 88, 44, 14, 56});
    display(number);

    cout << endl << "set的删除操作" << endl;
    it = number.end();
    --it;
    number.erase(it);
    display(number);

    //set没有下标访问运算符
    //不支持元素修改(红黑树)

}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

